<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Tutorial</title>
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Chapter 1. Boost.Log v2">
<link rel="up" href="../index.html" title="Chapter 1. Boost.Log v2">
<link rel="prev" href="design.html" title="Design overview">
<link rel="next" href="tutorial/trivial_filtering.html" title="Trivial logging with filters">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td></tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="design.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="tutorial/trivial_filtering.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="log.tutorial"></a><a class="link" href="tutorial.html" title="Tutorial">Tutorial</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="tutorial.html#log.tutorial.trivial">Trivial logging</a></span></dt>
<dt><span class="section"><a href="tutorial/trivial_filtering.html">Trivial logging with
      filters</a></span></dt>
<dt><span class="section"><a href="tutorial/sinks.html">Setting up sinks</a></span></dt>
<dt><span class="section"><a href="tutorial/sources.html">Creating loggers and writing logs</a></span></dt>
<dt><span class="section"><a href="tutorial/attributes.html">Adding more information to log:
      Attributes</a></span></dt>
<dt><span class="section"><a href="tutorial/formatters.html">Log record formatting</a></span></dt>
<dt><span class="section"><a href="tutorial/advanced_filtering.html">Filtering revisited</a></span></dt>
<dt><span class="section"><a href="tutorial/wide_char.html">Wide character logging</a></span></dt>
</dl></div>
<p>
      In this section we shall walk through the essential steps to get started with
      the library. After reading it you should be able to initialize the library
      and add logging to your application. The code of this tutorial is also available
      in examples residing in the <code class="computeroutput"><span class="identifier">libs</span><span class="special">/</span><span class="identifier">log</span><span class="special">/</span><span class="identifier">examples</span></code> directory. Feel free to play with
      them, compile and see the result.
    </p>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="log.tutorial.trivial"></a><a class="link" href="tutorial.html#log.tutorial.trivial" title="Trivial logging">Trivial logging</a>
</h3></div></div></div>
<p>
        For those who don't want to read tons of clever manuals and just need a simple
        tool for logging, here you go:
      </p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">log</span><span class="special">/</span><span class="identifier">trivial</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">char</span><span class="special">*[])</span>
<span class="special">{</span>
    <span class="identifier">BOOST_LOG_TRIVIAL</span><span class="special">(</span><span class="identifier">trace</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="string">"A trace severity message"</span><span class="special">;</span>
    <span class="identifier">BOOST_LOG_TRIVIAL</span><span class="special">(</span><span class="identifier">debug</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="string">"A debug severity message"</span><span class="special">;</span>
    <span class="identifier">BOOST_LOG_TRIVIAL</span><span class="special">(</span><span class="identifier">info</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="string">"An informational severity message"</span><span class="special">;</span>
    <span class="identifier">BOOST_LOG_TRIVIAL</span><span class="special">(</span><span class="identifier">warning</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="string">"A warning severity message"</span><span class="special">;</span>
    <span class="identifier">BOOST_LOG_TRIVIAL</span><span class="special">(</span><span class="identifier">error</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="string">"An error severity message"</span><span class="special">;</span>
    <span class="identifier">BOOST_LOG_TRIVIAL</span><span class="special">(</span><span class="identifier">fatal</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="string">"A fatal severity message"</span><span class="special">;</span>

    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
<p>
        <a href="../../../../../libs/log/example/doc/tutorial_trivial.cpp" target="_top">See the complete
        code</a>.
      </p>
<p>
        The <code class="computeroutput"><span class="identifier">BOOST_LOG_TRIVIAL</span></code> macro
        accepts a severity level and results in a stream-like object that supports
        insertion operator. As a result of this code, the log messages will be printed
        on the console. As you can see, this library usage pattern is quite similar
        to what you would do with <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span></code>.
        However, the library offers a few advantages:
      </p>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">
            Besides the record message, each log record in the output contains a
            timestamp, the current thread identifier and severity level.
          </li>
<li class="listitem">
            It is safe to write logs from different threads concurrently, log messages
            will not be corrupted.
          </li>
<li class="listitem">
            As will be shown later, filtering can be applied.
          </li>
</ol></div>
<p>
        It must be said that the macro, along with other similar macros provided
        by the library, is not the only interface the library offers. It is possible
        to issue log records without using any macros at all.
      </p>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2007-2021 Andrey Semashev<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>).
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="design.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="tutorial/trivial_filtering.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
